﻿package mortal.game.view.firstReCharge
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import __AS3__.vec.*;
    import baseEngine.modifiers.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.effectEditTool.temple.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.mvc.interfaces.*;

    public class FirstRechargeModule extends BaseWindow
    {
        private var _bg:GImageBitmap;
        private var _todayRewardBitmap:GImageBitmap;
        private var _reChargeBtn:GLoadingButton;
        private var _getRewardBtn:GLoadingButton;
        private var _onlyItemName:GTextFiled;
        private var _valuableItemName:GTextFiled;
        private var _img2d:Img2D;
        private var _rect3d:Rect3DObject;
        private var _bodyPlayer1:ActionPlayer;
        private var _bodyPlayer2:ActionPlayer;
        private var _effectPlayer1:EffectPlayer;
        private var _effectPlayer2:EffectPlayer;
        private var _isAddAction_1:Boolean;
        private var _isAddAction_2:Boolean;
        private var tween1:TweenMax;
        private var turnNum:Number = 0;
        private var _rewardList:Vector.<EffectItem>;
        private var _firstRechargeRewardList:Array;
        private var _serverOpenDay:int;
        public static var rewardArr:Array = [GameConst.TDay1FirstRechargeGiftCode, GameConst.TDay2FirstRechargeGiftCode, GameConst.TDay3FirstRechargeGiftCode, GameConst.TDay4FirstRechargeGiftCode, GameConst.TDay5FirstRechargeGiftCode, GameConst.TDay6FirstRechargeGiftCode, GameConst.TDay7FirstRechargeGiftCode, GameConst.TDay8FirstRechargeGiftCode, GameConst.TDay9FirstRechargeGiftCode, GameConst.TDay10FirstRechargeGiftCode, GameConst.TDay11FirstRechargeGiftCode];
        private static var _instance:FirstRechargeModule;

        public function FirstRechargeModule(param1:ILayer = null)
        {
            this._rewardList = new Vector.<EffectItem>;
            this._firstRechargeRewardList = [110141001, 100001102, 100001112, 100001122, 100001132];
            this.setSize(892, 600);
            super(param1);
            this.title = Language.getString(21477);
            this.layer = LayerManager.windowLayer3D;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.gImageBitmap(ImagesConst.FirstRechargeModuleBg, 0, 0, null, this.add2d);
            var _loc_1:* = ClockManager.instance.getLoginDayNumByPassZero();
            this._todayRewardBitmap = UIFactory.gImageBitmap("", 7, 37, this);
            var _loc_2:* = GlobalStyle.textFormatChen.center();
            _loc_2.size = 16;
            _loc_2.bold = true;
            this._onlyItemName = UIFactory.gTextField("", 37, 232, 200, 30, this.contentTopOf3DSprite, _loc_2);
            this._valuableItemName = UIFactory.gTextField("", 647, 232, 200, 30, this.contentTopOf3DSprite, _loc_2);
            this._reChargeBtn = UIFactory.gLoadingButton(ResFileConst.FirstRechargeBtn, 349, 499, 188, 50, contentTopOf3DSprite);
            this._reChargeBtn.configEventListener(MouseEvent.CLICK, this.gotoRecharge);
            this._getRewardBtn = UIFactory.gLoadingButton(ResFileConst.GetGiftBtn2, 293, 507, 112, 32, contentTopOf3DSprite);
            this._getRewardBtn.x = (this.width - this._getRewardBtn.width) * 0.5;
            this._getRewardBtn.configEventListener(MouseEvent.CLICK, this.getRewardHandler);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._onlyItemName.dispose(param1);
            this._onlyItemName = null;
            this._valuableItemName.dispose(param1);
            this._valuableItemName = null;
            this._bg.dispose(param1);
            this._bg = null;
            this._todayRewardBitmap.dispose(param1);
            this._todayRewardBitmap = null;
            this._reChargeBtn.dispose(param1);
            this._reChargeBtn = null;
            this._getRewardBtn.dispose(param1);
            this._getRewardBtn = null;
            if (this.tween1 && this.tween1.active)
            {
                this.tween1.kill();
            }
            if (this._img2d)
            {
                this._img2d.dispose(param1);
                this._img2d = null;
            }
            if (this._bodyPlayer1)
            {
                this._rect3d.disposeObj3d(this._bodyPlayer1);
                this._bodyPlayer1 = null;
            }
            if (this._bodyPlayer2)
            {
                this._rect3d.disposeObj3d(this._bodyPlayer2);
                this._bodyPlayer2 = null;
            }
            if (this._effectPlayer1)
            {
                this._rect3d.disposeObj3d(this._effectPlayer1);
                this._effectPlayer1 = null;
            }
            if (this._effectPlayer2)
            {
                this._rect3d.disposeObj3d(this._effectPlayer2);
                this._effectPlayer2 = null;
            }
            if (this._rect3d)
            {
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            this._rewardList.length = 0;
            this._serverOpenDay = 0;
            super.disposeImpl(param1);
            return;
        }// end function

        public function set serverOpenDay(param1:int) : void
        {
            this._serverOpenDay = param1;
            this.updateReward();
            this.updateFirstRechargeInfo(Cache.instance.firstRecharge.firstRechargeRewardInfo);
            return;
        }// end function

        private function add2d() : void
        {
            var _loc_1:BitmapData = null;
            if (this.isDisposed)
            {
                return;
            }
            this.add3d();
            if (this._rect3d)
            {
                _loc_1 = this._bg.bitmapData;
                this._img2d = new Img2D(null, _loc_1, new Rectangle(0, 0, 878, 435), false);
                this._rect3d.addImg(this._img2d);
            }
            this.update3DModel();
            return;
        }// end function

        private function add3d() : void
        {
            this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(8, 165, 878, 435), this);
            Rect3DManager.instance.windowShowHander(null, this);
            return;
        }// end function

        private function update3DModel() : void
        {
            var _loc_4:TModelShow = null;
            var _loc_5:String = null;
            var _loc_6:String = null;
            var _loc_7:String = null;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_1:* = this._serverOpenDay - 1;
            var _loc_2:* = ItemConfig.instance.getConfig(rewardArr[_loc_1]) as ItemGiftBagInfo;
            if (_loc_2 == null)
            {
                return MsgManager.showRollTipsMsg(Language.getStringByParam(21478, this._serverOpenDay));
            }
            var _loc_3:* = Cache.instance.firstRecharge.firstRechargeRewardInfo;
            _loc_4 = PlayerModelConfig.instance.getModelShowInfo(_loc_2.effect, Cache.instance.role.entityInfo.sex, Cache.instance.role.entityInfo.career);
            if (_loc_4)
            {
                if (_loc_4.isAddAction)
                {
                    this._isAddAction_1 = true;
                }
                else
                {
                    this._isAddAction_1 = false;
                }
                if (_loc_4.rotation)
                {
                    _loc_8 = _loc_4.rotation.split("#")[0];
                    _loc_9 = _loc_4.rotation.split("#")[1];
                }
                this._onlyItemName.text = _loc_4.name;
                _loc_5 = _loc_4.mesh + ".md5mesh";
                _loc_6 = _loc_4.bone + ".skeleton";
                _loc_7 = _loc_4.texture;
                if (this._bodyPlayer1)
                {
                    this._rect3d.disposeObj3d(this._bodyPlayer1);
                    this._bodyPlayer1 = null;
                }
                this._bodyPlayer1 = FrObjectPool.getObject(ActionPlayer, this);
                this._bodyPlayer1.enabledPool = false;
                this._bodyPlayer1.load(_loc_5, _loc_6, _loc_7);
                var _loc_10:* = _loc_4.scale * 0.01;
                this._bodyPlayer1.scaleZ = _loc_4.scale * 0.01;
                var _loc_10:* = _loc_10;
                this._bodyPlayer1.scaleY = _loc_10;
                this._bodyPlayer1.scaleX = _loc_10;
                this._bodyPlayer1.rotationZ = _loc_9;
                this._bodyPlayer1.rotationY = _loc_8;
                this._bodyPlayer1.play();
                this._rect3d.addObject3d(this._bodyPlayer1, _loc_4.pos.split("#")[0], _loc_4.pos.split("#")[1]);
                if (_loc_4.actionName)
                {
                    this._bodyPlayer1.changeAction(_loc_4.actionName);
                }
                if (this._effectPlayer1)
                {
                    this._rect3d.disposeObj3d(this._effectPlayer1);
                    this._effectPlayer1 = null;
                }
                this._effectPlayer1 = EffectPlayerPool.instance.getEffectPlayer(_loc_4.selfEffect, this._bodyPlayer1, this._rect3d.renderList, false);
                this._effectPlayer1.play(PlayMode.ANIMATION_LOOP_MODE);
                if (this._effectPlayer1.temple is TempleRole)
                {
                    (this._effectPlayer1.temple as TempleRole).setRoleParams(this._bodyPlayer1, null, null);
                }
                this.tween1 = TweenMax.to(this._bodyPlayer1, 0.01, {ease:Quint.easeIn, repeat:-1, onRepeat:this.modelTurn});
            }
            _loc_4 = PlayerModelConfig.instance.getModelShowInfo(_loc_2.effectEx, Cache.instance.role.entityInfo.sex, Cache.instance.role.entityInfo.career);
            if (_loc_4)
            {
                if (_loc_4.isAddAction)
                {
                    this._isAddAction_2 = true;
                }
                else
                {
                    this._isAddAction_2 = false;
                }
                if (_loc_4.rotation)
                {
                    _loc_8 = _loc_4.rotation.split("#")[0];
                    _loc_9 = _loc_4.rotation.split("#")[1];
                }
                this._valuableItemName.text = _loc_4.name;
                _loc_5 = _loc_4.mesh + ".md5mesh";
                _loc_6 = _loc_4.bone + ".skeleton";
                _loc_7 = _loc_4.texture;
                if (this._bodyPlayer2)
                {
                    this._rect3d.disposeObj3d(this._bodyPlayer2);
                    this._bodyPlayer2 = null;
                }
                this._bodyPlayer2 = FrObjectPool.getObject(ActionPlayer, this);
                this._bodyPlayer2.enabledPool = false;
                this._bodyPlayer2.load(_loc_5, _loc_6, _loc_7);
                this._bodyPlayer2.play();
                this._bodyPlayer2.rotationZ = _loc_9;
                this._bodyPlayer2.rotationY = _loc_8;
                var _loc_10:* = _loc_4.scale * 0.01;
                this._bodyPlayer2.scaleZ = _loc_4.scale * 0.01;
                var _loc_10:* = _loc_10;
                this._bodyPlayer2.scaleY = _loc_10;
                this._bodyPlayer2.scaleX = _loc_10;
                this._rect3d.addObject3d(this._bodyPlayer2, _loc_4.pos.split("#")[0], _loc_4.pos.split("#")[1]);
                if (_loc_4.actionName)
                {
                    this._bodyPlayer2.changeAction(_loc_4.actionName);
                }
                if (this._effectPlayer2)
                {
                    this._rect3d.disposeObj3d(this._effectPlayer2);
                    this._effectPlayer2 = null;
                }
                this._effectPlayer2 = EffectPlayerPool.instance.getEffectPlayer(_loc_4.selfEffect, this._bodyPlayer2, this._rect3d.renderList, false);
                this._effectPlayer2.play(PlayMode.ANIMATION_LOOP_MODE);
                if (this._effectPlayer2.temple is TempleRole)
                {
                    (this._effectPlayer2.temple as TempleRole).setRoleParams(this._bodyPlayer2, null, null);
                }
            }
            return;
        }// end function

        private function modelTurn() : void
        {
            if (this._bodyPlayer1 && this._isAddAction_1)
            {
                this._bodyPlayer1.y = this._bodyPlayer1.y + Math.cos(this.turnNum) * 0.8;
                this._bodyPlayer1.rotationY = this._bodyPlayer1.rotationY + 2;
            }
            if (this._bodyPlayer2 && this._isAddAction_2)
            {
                this._bodyPlayer2.y = this._bodyPlayer2.y + Math.cos(this.turnNum) * 0.8;
                this._bodyPlayer2.rotationY = this._bodyPlayer2.rotationY + 2;
            }
            this.turnNum = this.turnNum + 0.05;
            return;
        }// end function

        private function getRewardHandler(event:MouseEvent) : void
        {
            GameProxy.role.getDayFirstReward();
            return;
        }// end function

        private function gotoRecharge(event:MouseEvent) : void
        {
            GameUtil.gotoRecharge(ModuleTypeConst.FirstRecharge);
            return;
        }// end function

        public function updateReward() : void
        {
            var _loc_4:Array = null;
            var _loc_5:String = null;
            var _loc_6:ItemData = null;
            var _loc_7:Array = null;
            var _loc_8:ItemData = null;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            var _loc_12:int = 0;
            var _loc_13:int = 0;
            var _loc_14:int = 0;
            var _loc_15:int = 0;
            var _loc_16:int = 0;
            var _loc_17:EffectItem = null;
            var _loc_1:* = Cache.instance.firstRecharge.firstRechargeRewardInfo;
            var _loc_2:* = this._serverOpenDay - 1;
            this._todayRewardBitmap.imgUrl = "firstRecharge_" + (_loc_2 + 1) + ".jpg";
            var _loc_3:* = ItemConfig.instance.getConfig(rewardArr[_loc_2]) as ItemGiftBagInfo;
            if (_loc_3)
            {
                _loc_4 = _loc_3.itemList1.split("&");
                if (_loc_1.equipIsGet == false)
                {
                    for each (_loc_16 in this._firstRechargeRewardList)
                    {
                        
                        _loc_6 = new ItemData(_loc_16);
                        if (ItemsUtil.isFitCarrer(_loc_6))
                        {
                            _loc_5 = "3#" + _loc_16 + "#1";
                            _loc_4.unshift(_loc_5);
                        }
                    }
                }
                _loc_9 = _loc_4.length > 6 ? (6) : (_loc_4.length);
                while (_loc_10 < _loc_9)
                {
                    
                    if (_loc_4[_loc_10])
                    {
                        _loc_7 = _loc_4[_loc_10].split("#");
                        _loc_8 = new ItemData(int(_loc_7[1]));
                        if (_loc_8.itemInfo && ItemsUtil.isFitCarrer(_loc_8))
                        {
                            if (ItemsUtil.isEquip(_loc_8))
                            {
                                _loc_8.extInfo = new ItemExInfo();
                                _loc_8.extInfo.strengthen = 5;
                                _loc_8.serverData.playerId = 1;
                            }
                            _loc_17 = UICompomentPool.getUICompoment(EffectItem);
                            _loc_17.setItemStyle(ItemStyleConst.Big, ImagesConst.RewardItemBigBg, 4, 4);
                            _loc_17.isDragAble = false;
                            _loc_17.isDropAble = false;
                            _loc_17.isShowLock = true;
                            _loc_17.itemData = _loc_8;
                            _loc_17.amount = _loc_7[2];
                            _contentTopOf3DSprite.addChild(_loc_17);
                            this.pushUIToDisposeVec(_loc_17);
                            this._rewardList.push(_loc_17);
                        }
                    }
                    _loc_10++;
                }
                _loc_9 = this._rewardList.length;
                _loc_11 = 80;
                _loc_12 = 3;
                _loc_10 = 0;
                while (_loc_10 < _loc_9)
                {
                    
                    _loc_15 = int(_loc_10 / _loc_12);
                    _loc_13 = _loc_9 - _loc_15 * 3 >= 3 ? (3) : (_loc_9 - _loc_15 * 3);
                    _loc_14 = (this.width - _loc_13 * 80 + 15) * 0.5;
                    this._rewardList[_loc_10].x = _loc_14 + _loc_11 * (_loc_10 % 3);
                    this._rewardList[_loc_10].y = 335 + _loc_15 * _loc_11;
                    this._rewardList[_loc_10].showEffect(true, 36, 32, "ArriundEffect_cheng_big.swf");
                    _loc_10++;
                }
            }
            return;
        }// end function

        public function updateFirstRechargeInfo(param1:SDayFirstRechargeRewardInfo) : void
        {
            if (param1.rechargeNum > 0 && !param1.isGet)
            {
                this._getRewardBtn.visible = true;
                this._reChargeBtn.visible = false;
            }
            else
            {
                this._getRewardBtn.visible = false;
                this._reChargeBtn.visible = true;
            }
            return;
        }// end function

        public static function get instance() : FirstRechargeModule
        {
            if (_instance == null)
            {
                _instance = new FirstRechargeModule;
            }
            return _instance;
        }// end function

        public static function get isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
